/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Plant;
import org.crystalrange.enterprise.service.PlantService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class PlantListController extends BaseController {

    protected PlantService plantService;
    protected Plant selectedPlant;
    protected Listbox listboxPlant;
    protected Search searchPlant;
    protected List<Plant> plants;

    public PlantService getPlantService() {
        return this.plantService;
    }

    public void setPlantService(PlantService plantService) {
        this.plantService = plantService;
    }

    public Plant getSelectedPlant() {
        return this.selectedPlant;
    }

    public void setSelectedPlant(Plant selectedPlant) {
        this.selectedPlant = selectedPlant;
    }

    public List<Plant> getPlants() {
        return this.plants;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchPlant = new Search(Plant.class);
        this.searchPlant.setMaxResults(100);
        this.searchPlant.addFetch("companyCode");
        this.searchPlant.addFetch("createdBy");
        this.searchPlant.addFetch("updatedBy");
        this.plants = this.plantService.search(this.searchPlant);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemPlantCreate(Event event) throws Exception {
        this.plantCreate();
    }

    public void onClick$menuitemPlantEdit(Event event) throws Exception {
        this.plantEdit();
    }

    public void onClick$menuitemPlantDelete(Event event) throws Exception {
        this.plantDelete();
    }

    public void onClick$menuitemPlantSearch(Event event) throws Exception {
        this.plantSearch();
    }

    public void onClick$menuitemPlantRefresh(Event event) throws Exception {
        this.plantRefresh();
    }

    public void onClick$toolbarbuttonPlantCreate(Event event) throws Exception {
        this.plantCreate();
    }

    public void onClick$toolbarbuttonPlantEdit(Event event) throws Exception {
        this.plantEdit();
    }

    public void onClick$toolbarbuttonPlantDelete(Event event) throws Exception {
        this.plantDelete();
    }

    public void onClick$toolbarbuttonPlantSearch(Event event) throws Exception {
        this.plantSearch();
    }

    public void onClick$toolbarbuttonPlantRefresh(Event event) throws Exception {
        this.plantRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.plantEdit();
    }
    
    private void plantCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/plant.zul");
    }

    private void plantEdit() throws Exception {
        if (this.selectedPlant != null) {
            this.execution.sendRedirect("~./zul/html/plant.zul?Id=" + this.selectedPlant.getId().toString());
        }
    }

    private void plantDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxPlant.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Plant plant = (Plant) selectedItem.getValue();
                    this.plantService.remove(plant);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.plants = this.plantService.search(this.searchPlant);
        this.binder.loadAll();
    }

    private void plantSearch() throws Exception {
        this.execution.setAttribute("searchPlant", this.searchPlant);

        Window window = (Window) this.execution.createComponents("~./zul/html/plantSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchPlant = (Search) this.execution.getAttribute("searchPlant");
        }

        this.plants = this.plantService.search(this.searchPlant);
        this.binder.loadAll();
    }

    private void plantRefresh() throws Exception {
        this.plants = this.plantService.search(this.searchPlant);
        this.binder.loadAll();
    }
}
